﻿using EYangFramewirk.Business.SystemManager.Interfaces;
using EYangFramework.Domain.Entitys.Entrys;
using EYangFramework.Domain.Entitys.ValueObjects;
using EYangFramework.Domain.Repositorys;
using EYangFramework.Domain.Services;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EYangFramewirk.Business.SystemManager.Implementation
{
    public class BLLAuthManager : IBLLAuthManager
    {
        private IAuthManagerService AuthManagerService { get; set; }
        private IRepository<Role> RepositoryRole { get; set; }
        private IRepository<User> RepositoryUser{ get; set; }
        private IRepository<Navigation> RepositoryNavigation { get; set; }
        public BLLAuthManager(IAuthManagerService authManagerService, IRepository<Role> repositorRole, IRepository<User> repositoryUser, IRepository<Navigation> repositoryNavigation)
        {
            this.AuthManagerService = authManagerService;
            this.RepositoryRole = repositorRole;
            this.RepositoryUser = repositoryUser;
            this.RepositoryNavigation = repositoryNavigation;
        }
        public void Dispose()
        {
            AuthManagerService.Dispose();//释放掉DbContext
            RepositoryRole.DbContext.Dispose();
            RepositoryUser.DbContext.Dispose();
        }

        public List<Role> GetAllRoles()
        {
            //throw new NotImplementedException();
            return RepositoryRole.GetAll().Include(t => t.PARENT).Include(t => t.NAVIGATIONS).Include(t => t.MENUS).AsNoTracking().Where(t => true).ToList();//获取到所有的角色
        }

        public List<User> GetAllUsers()
        {
            //throw new NotImplementedException();
            return RepositoryUser.GetAll().Include(t => t.LEADER).Include(t => t.ROLE).Include(t => t.NAVIGATIONS).Include(t => t.MENUS).AsNoTracking().Where(t => !t.ISDELETE).ToList();//获取到所有的用户
        }

        public bool UpdateRoleMenus(List<Menu> selectedMenus, string roleName)
        {
            return AuthManagerService.UpdateRoleMenus(selectedMenus, roleName);
        }

        public bool UpdateRoleNavigations(List<Navigation> selectedNavigations, string roleName)
        {
            return AuthManagerService.UpdateRoleNavigations(selectedNavigations, roleName);
        }

        public bool UpdateUserNavigations(List<Navigation> selectedNavigations, string userName)
        {
            return AuthManagerService.UpdateUserNavigations(selectedNavigations, userName);
        }

        public bool UpdatUserMenus(List<Menu> selectedMenus, string userName)
        {
            return AuthManagerService.UpdatUserMenus(selectedMenus, userName);
        }

        public List<Navigation> GetAllNavigations()
        {
            return RepositoryNavigation.GetAll().Include(t => t.PREVIEWNAVIGATION).AsNoTracking().Where(t => true).ToList();//获取所有的导航
        }
    }
}
